package cn.tedu.knows.faq.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TimeAspect {
    //定义切面
    //通配service包下所有接口的所有方法
    @Pointcut("execution(public * cn.tedu.knows.faq.service.*.*(..))")
    public void timer(){}

    @Around("timer()")
    public Object timeRecord(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();//开始时间
        Object object = joinPoint.proceed();//调用方法
        long end = System.currentTimeMillis(); //结束时间
        long time = end-start;

        //获得方法名
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法用时:"+time);
        return object;
    }
}
